import React, {useEffect} from 'react';
import {Button, Col, DatePicker, Form, Input, Modal, Row, Select} from 'antd';
import {formInputRules, formTextAreaRules, phoneRules} from '@/utils/rules';
import {addOrModifyExpert, queryWarehouse} from '@/services/emergency';
import {useRequest} from "ahooks";
import {filterEmpty} from '@/utils/methods';
import moment from "dayjs";

const EditReport: React.FC<any> = ({expertId, onSuccess, dispatch, ...props}: any) => {
  const [form] = Form.useForm();

  const {
    run: runSubmit,
    loading: loadingSubmit
  } = useRequest((values: any) => {
    const params = filterEmpty({
      originValues: values,
      keepEmpty: true,
    });
    expertId && (params.id = expertId);
    params.bornDate &&
    (params.bornDate = params.bornDate.format('YYYY-MM-DD HH:mm:ss'));
    params.workTime &&
    (params.workTime = params.workTime.format('YYYY-MM-DD HH:mm:ss'));
    return addOrModifyExpert(params)
  }, {
    manual: true,
    onSuccess(res: any) {
      dispatch({
        clear: true,
        open: false
      })
      onSuccess && onSuccess(res);
    }
  });
  const {
    run: runDetail,
  } = useRequest(() => {
    return queryWarehouse({
      id: expertId,
    }).then((res: any) => {
      form.setFieldsValue({
        name: res.name,
        sex: res.sex,
        nation: res.nation,
        bornDate: moment(res.bornDate),
        politicalStatus: res.politicalStatus,
        healthCondition: res.healthCondition,
        workTime: moment(res.workTime),
        job: res.job,
        jobTitle: res.jobTitle,
        educationRecord: res.educationRecord,
        graduatedSchool: res.graduatedSchool,
        speciality: res.speciality,
        workAddress: res.workAddress,
        goodAt: res.goodAt,
        researchDirection: res.researchDirection,
        homeAddress: res.homeAddress,
        phoneNumber: res.phoneNumber,
        departmentTel: res.departmentTel,
        introduce: res.introduce,
      });
      return res;
    })
  }, {
    manual: true
  })


  useEffect(() => {
    if (props.open) {
      if (expertId) {
        Promise.resolve().then(() => {
          runDetail();
        })
      }
    } else {
      form.resetFields();
    }
  }, [props.open]);
  return (
    <Modal
      {...props}
      onCancel={() => {
        dispatch({
          clear: true,
          open: false
        })
      }}
      footer={[
        <Button key="cancel" onClick={() => {
          dispatch({
            clear: true,
            open: false
          })
        }}>
          关闭
        </Button>,
        <Button
          type="primary"
          key="submit"
          loading={loadingSubmit}
          onClick={async () => {
            const value: any = await form.validateFields()
            runSubmit(value)
          }}
        >
          保存
        </Button>,
      ]}
    >
      <Form form={form} labelCol={{span: 9, offset: 0}}>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="name"
              label="姓名"
              rules={[
                ...formInputRules,
                {
                  required: true,
                  message: '请填写姓名',
                },
              ]}
            >
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              label="性别"
              name="sex"
              style={{marginBottom: '0'}}
              rules={[
                {
                  required: true,
                  message: '请选择性别',
                },
              ]}
            >
              <Select
                placeholder="请选择"
                options={[{
                  label: '男',
                  value: 1
                }, {
                  label: '女',
                  value: 2
                }]}
              ></Select>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="nation"
              label="名族"
              rules={[
                ...formInputRules,
                {
                  required: true,
                  message: '请填写名族',
                },
              ]}
            >
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name="bornDate"
              label="出生日期"
              rules={[
                {
                  required: true,
                  message: '请选择出生日期',
                },
              ]}
            >
              <DatePicker/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="politicalStatus"
              label="政治面貌"
              rules={[...formInputRules]}
            >
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name="healthCondition"
              label="健康状态"
              rules={[...formInputRules]}
            >
              <Input/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="workTime"
              label="参加工作时间"
              rules={[
                {
                  required: true,
                  message: '请填写参加工作时间',
                },
              ]}
            >
              <DatePicker/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item name="job" label="行政职务" rules={[...formInputRules]}>
              <Input/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item name="jobTitle" label="职称" rules={[...formInputRules]}>
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item name="educationRecord" label="学历">
              <Select
                placeholder="请选择"
                options={[{
                  label: '其他',
                  value: '0',
                },
                  {
                    label: '博士',
                    value: '1',
                  },
                  {
                    label: '硕士',
                    value: '2',
                  },
                  {
                    label: '本科',
                    value: '3',
                  },
                  {
                    label: '大专',
                    value: '4',
                  },
                  {
                    label: '中专',
                    value: '5',
                  },]}
              ></Select>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="graduatedSchool"
              label="毕业院校"
              rules={[...formInputRules]}
            >
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name="speciality"
              label="专业"
              rules={[
                ...formInputRules,
                {
                  required: true,
                  message: '请填写专业',
                },
              ]}
            >
              <Input/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="workAddress"
              label="工作单位"
              rules={[
                ...formInputRules,
                {
                  required: true,
                  message: '请填写工作单位',
                },
              ]}
            >
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name="goodAt"
              label="擅长事故类型"
              rules={[
                ...formInputRules,
                {
                  required: true,
                  message: '请填写擅长事故类型',
                },
              ]}
            >
              <Input/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="researchDirection"
              label="主要研究方向"
              rules={[...formInputRules]}
            >
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name="homeAddress"
              label="家庭住址"
              rules={[...formInputRules]}
            >
              <Input/>
            </Form.Item>
          </Col>
        </Row>
        <Row gutter={24}>
          <Col span={12}>
            <Form.Item
              name="phoneNumber"
              label="手机号码"
              validateTrigger="onBlur"
              rules={[
                ...phoneRules,
                {
                  required: true,
                  message: '请填写手机号码',
                },
              ]}
            >
              <Input/>
            </Form.Item>
          </Col>
          <Col span={12}>
            <Form.Item
              name="departmentTel"
              label="单位号码"
              validateTrigger="onBlur"
              rules={[
                ...phoneRules,
                {
                  required: true,
                  message: '请填写单位号码',
                },
              ]}
            >
              <Input/>
            </Form.Item>
          </Col>
        </Row>
        <div style={{paddingLeft: '11px'}}>
          <Form.Item
            name="introduce"
            label="个人简历"
            labelCol={{span: 4}}
            rules={[...formTextAreaRules]}
          >
            <Input.TextArea/>
          </Form.Item>
        </div>
      </Form>
    </Modal>
  );
}

export default EditReport;
